<!--
* description: 发票登记
* author: 
* createdDate: 2021-05-06
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :key="oneTableKey"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-table-cols="tableCols"
      :dynamic-form-fields="formField"
      :dynamic-is-show-select="true"
      :dynamic-is-init-table="true"
      :dynamic-is-show-more-btn="true"
      :dynamic-api-style="'restful'"
      :dynamic-cell-style="columnStyle"
      :callback-obj="callbackObj"
    />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import OneTableTemplate from "@/components/templates/oneTable";
import api from '@/api/se/maintenanceManagement/invoiceRegister'
import { orgApis } from "@/api/caseManagementQuery";
import { exportExcel } from '@/utils/getTimeString.js'
import Vue from "vue";

export default {
  name: "invoiceRegister",

  components: {
    OneTableTemplate,
    //Edit: () => import("./edit"),
  },
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],

  // 阻塞路由预加载网格中组件的数据
  beforeRouteEnter(to, from, next) {
    Vue.prototype.$ConfigCache.CacheConfig.initData([to.path, ""], function () {
      next();
    });
  },

  data() {
    // 页面标识（与上面beforeRouteEnter中的对应）
    const viewPageCode = this.$route.path;

    // 绑定事件配置化事件
    this.$ConfigCache.CacheConfig.bindEvent(this, [viewPageCode]);

    return {
      icon: "el-icon-arrow-down",
      status: false,
      selectiondata: [],
      // 网格查询API配置对象
      apiConfig: api.queryInvoiceRegister,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          name: "search",
          clickEvent: () => this.queryTable(1),
          text: this.$t("sys.button.query"), // 查询
          position: "right",
          fuzzySearch: true,
        },
        {
          compKey: "btnKey2",
          type: "",
          size: "small",
          disabled: true,
          parentFileds: "disabled:table",
          clickEvent: () => this.makeInvoice(),
          text: "开票", 
          name: "adopt",
          position: "left",
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          clickEvent: () => this.reset(),
          text: this.$t("sys.button.reset"),
          position: "right", // 重置
        },
      ],

      // 动态组件-查询条件
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
              .tableComponents
          : [  
              {
                compKey: 'compKey1',
                labelName: '门店编码：',
                codeField: 'dlrName',
                textField: 'dlrCode',
                component: () => import('@/views/ve/planReport/components/multipleSearchInput'),
                type: 'inputText',
                isMust: true,
                // isRequire: true,
                dataToObject: {
                  // 控制弹窗页面 搜索 网格 元素
                  tableComponents: [
                    {
                      compKey: "comKey1",
                      labelName: "网点编码：",
                      codeField: "dlrCode",
                      component: () => import("@/components/org/commonInput"),
                      type: "inputText",
                      isMust: true,
                      span: 8,
                    },
                    {
                      compKey: "comKey2",
                      labelName: "网点名称：",
                      codeField: "dlrShortName",
                      component: () => import("@/components/org/commonInput"),
                      type: "inputText",
                      isMust: true,
                      span: 8,
                    },
                  ],
                  tableCols: [
                    {
                      prop: "dlrCode",
                      label: "网点编码",
                    },
                    {
                      prop: "dlrShortName",
                      label: "网点名称",
                    },
                  ],
                  formField: {
                    dlrShortName: '',
                    dlrCode: ''
                  },
                  oFields: 'dlrShortName,dlrCode',
                  returnField: {
                    prop: "dlrCode",
                    label: "网点编码",
                  },
                  api: orgApis.queryOrgDlrCodes
                },
                span: 6
              },  
              {
                compKey: "compKey2",
                labelName: "收款/结算日期：",
                codeField: "timeStart,timeEnd",
                component: () =>
                  import("@/components/org/datePicker/andtDatePicker"),
                type: "YYYY-MM-DD",
                // dataToObject: { timer: true, thirtyTime: true, hhmmss: true },
                format: "yyyy-MM-dd",
                isMul: false,
                isMust: true,
              },     
              {
                compKey: "compKey3",
                labelName: "派工/结算单号：",
                codeField: "repairAndBalanceCode",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isMul: false,
                isMust: true,
              },
              {
                compKey: "compKey4",
                labelName: "VIN码：",
                codeField: "vin",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isMul: false,
                isMust: true,
              },
              {
                compKey: "compKey5",
                labelName: "应收单位：",
                codeField: "receivableunit",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isMul: false,
                isMust: false,
              },
              {
                compKey: "compKey6",
                labelName: "支付方式：",
                codeField: "payment",
                lookuptype: "SE2004",
                customObj: {
                            api: api.GetLookupValue,
                            defOptionFields:['lookupValueCode','lookupValueName']
                        },
                component: () => import("@/components/org/commonSelect"),
                type: "dropdownList",
                isMul: false,
                isMust: false,
              },
              {
                compKey: "compKey7",
                labelName: "开票状态：",
                codeField: "invoiceState",
                component: () => import("@/components/org/LookupValue"),
                oFields: "statusId,statusText",
                options: [
                  { statusId: "0", statusText: "未开票" },
                  { statusId: "1", statusText: "已开票" },
                  { statusId: "2", statusText: "已冲红" },
                  { statusId: "-1", statusText: "已作废" },
                ],
                type: "dropdownList",
                isMul: true,
                isMust: false,
              },
              {
                compKey: "compKey8",
                labelName: "发票号：",
                codeField: "invoiceNo",
                component: () => import("@/components/org/commonInput"),
                type: "inputText",
                isMul: false,
                isMust: false,
              },
            ],

      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
              {
              prop: 'billId',
              label: '单据ID',
              hidden: true,
              alwaysHidden: true
              },
              {
                prop: "dlrCode",
                label: "门店编码",
              },
              {
                prop: "dlrName",
                width: 130,
                label: "门店名称",
              },

              {
                prop: "repairAndBalanceCode",
                width: 130,
                label: "派工/结算单号",
              },
              {
                prop: "vin",
                width: 150,
                label: "VIN码",
              },
              {
                prop: "repairAndBalanceDate",
                width: 130,
                label: "收款/结算时间",
              },
              {
                prop: "receivableunit",
                label: "应收单位",
              },
              {
                prop: "payment",
                label: "支付方式",
              },
              {
                prop: "amount",
                label: "结算金额",
              },
              {
                prop: "invoiceState",
                label: "开票状态",
              },
              {
                prop: "invoiceNo",
                label: "发票号",
              },
              {
                prop: "invoiceRegisterDate",
                width: 130,
                label: "开票时间",
              },
              {
                prop: "invoiceMan",
                label: "开票人",
              },
              {
                prop: "redInvoiceNo",
                label: "冲红发票号",
              },
              {
                prop: "redInvoiceDate",
                width: 130,
                label: "冲红时间",
              },
            ],
      callbackObj: {
        // 手动勾选时
        onCheckSelect: (selection, row) => {
          this.selectiondata = selection;
        },
        onCheckSelectChange: (selection) => {
          this.onCheckSelectChange1(selection);
        },
      },
      // 表单查询数据
      formField: this.$utils.getFormField(
        this,
        {
          dlrCode: "",
          timeStart: "",
          timeEnd: "",
          repairAndBalanceCode: "",
          vin: "",
          receivableunit: "",
          payment:'',
          invoiceState:'',
          invoiceNo:''
        },
        this.$ConfigCache.CacheConfig.cacheData[viewPageCode]
      ),
    };
  },
  watch: {
    "formField.orderStatus": {
      deep: true,
      handler: function (newV, oldV) {
        console.log("watch中：", newV);
        // this.querybystatus(newV)
        this.queryTable();
      },
    },
  },
  mounted() {
    // 列表查询
    this.queryTable(1)
  },

  methods: {
    onCheckSelectChange1(selection) {
      var btn1 = this.tableButtons[1];
      const isCheck = selection && selection.length > 0;
      btn1.disabled = !isCheck;
    },
    // 开票
    makeInvoice() {   
      return; //开票需要调用外部系统接口，目前还没有
      const that = this

      this.$refs.multipleTable.listLoading = true
        const queryObj = {
      // api配置
      apiConfig: api.pricePartsReviePass,
      // 条件/实体参数（变量），根据typeParam中的定义配置
      variables: {
        // 当前中台使用的名称有dataInfo、info，具体查看API文档
        paramList: this.selectiondata
       }
      }
      // 转换了中台请求格式数据
      var paramD = that.$getParams(queryObj)
      // 调用中台API方法（可复用）
      that.$requestAPI(paramD).then(response => {
        this.$message({
          message: response.msg,
          duration: 2000
        })
        if (response.result === '1') {
          this.$refs.multipleTable.listLoading = false
          this.queryTable()
        }
      })
      
    },
  },
};
</script>
<style lang='scss' scoped>
</style>